#include "user_capture.h"


//--------摄像头---------------------------------------------
user_capture::user_capture(QObject *parent): QThread{parent}
{
    is_video_saving = false;
    is_cap_start = false;

}

void user_capture::run()
{
    while(1){
        if(is_cap_start){
            user_img_read_processing();
        }
        waitKey(10);
    }
}


/*
    打开摄像头
*/
bool user_capture::user_cap_open(int index)
{
    return CVcap.open(index);
}

/*
    开始视频流
*/
bool user_capture::user_cap_start()
{
    //使用线程
    is_cap_start = true;
    return true;
}

/*
    停止视频流
*/
void user_capture::user_cap_stop()
{
    is_cap_start = false;
}

/*
    读取图像回调函数
*/
void user_capture::user_img_read_processing(void)
{
    CVcap.read(src_image);
    if(src_image.empty()){
        return;
    }

    if(is_video_saving){
        if(VWriter.isOpened()==true){
            VWriter.write(src_image);
            qDebug("saving video");
        }else{
            qDebug("saved video false");
        }
    }

    //------加入测试代码,画几个圆形在移动测试-----
    #if 1
    static int x = 420, y = 300, r = 80;
    // x += 5;
    // if(x>390){
    //     x = 200;
    // }
    Mat testCircle(480, 640, CV_8UC3, Scalar(0, 0, 0));
    circle(testCircle, Point(x, y),         r + 20,     Scalar(220, 240, 220), -1);
    // ellipse(testCircle, RotatedRect(Point2f(x, y), Size2f(150, 154), 0), Scalar(220, 240, 220), -1);
    circle(testCircle, Point(x-100, y-180), r,          Scalar(220, 240, 220), -1);
    src_image = testCircle.clone();
    #endif
    //===============================================


    imshow("srcImg-show", src_image);//

    //------------------图象处理过程------------------
    if(cameraParm.isCalib==false && cameraParm.calibMode==kzfCaptureParm::circleAutoMode){
        //圆形检测
        Mat src_pre = circleD.RectCirclePre(src_image);
        circleLists.clear();
        circleLists = circleD.AreaCircles(src_pre, src_image);

        //检测到新的圆形数据 转换成qt的QRectF数据,传给win转item
        cRects.clear();
        foreach (kzfCirclebox c, circleLists) {
            cRects.append(QRectF(c.centerPoint.x()-c.rect.width()/2, c.centerPoint.y()-c.rect.height()/2, c.rect.width(), c.rect.height()));
        }

        //识别到圆 发出信号更新
        int num = cRects.length();
        emit user_circle_got(num);
    }
    //===============================================


    //图象处理完成 发送信号
    cvtColor(src_image, src_image, COLOR_BGR2RGB);//需要转换成RGB
    emit user_img_done(QPixmap::fromImage(QImage(src_image.data, src_image.cols, src_image.rows, QImage::Format_RGB888)));
}

/*
    关闭摄像头
*/
void user_capture::user_cap_close()
{
    is_cap_start = true;
    CVcap.release();
}

/*
    查找摄像头
*/
QList<QString> user_capture::user_cap_scan()
{
    QList<QCameraInfo> infos =  QCameraInfo::availableCameras();
    QList<QString> capName;

    foreach(QCameraInfo info , infos){
        //打印一下摄像头的设备名称和设备信息
        // qDebug()<<info.deviceName()<<info.description();
        // qDebug("--------------------------------------");
        //写入列表中显示
        //deviceName--QCamera打开摄像头使用
        //description--摄像头名称
        capName.append(info.description());
    }

    return capName;
}

/*
    摄像头是否打开
*/
bool user_capture::is_user_cap_opened()
{
    return CVcap.isOpened();
}

/*
    调用cv读取参数
*/
double user_capture::user_get(int propId)
{
    return CVcap.get(propId);
}

/*
    调用cv设置参数
*/
bool user_capture::set(int propId, double value)
{
    return CVcap.set(propId, value);
}

/*
    获取摄像头参数
*/
void user_capture::user_capture::user_get_cap_parm()
{
    //读取摄像头参数
    cout<<CVcap.getBackendName()<<endl;
    cout<<CVcap.get(CV_CAP_PROP_FRAME_WIDTH)<<endl;
    cout<<CVcap.get(CV_CAP_PROP_FRAME_HEIGHT)<<endl;
    cout<<CVcap.get(CV_CAP_PROP_FPS)<<endl;
    cout<<CVcap.get(CV_CAP_PROP_BRIGHTNESS)<<endl;
    cout<<CVcap.get(CV_CAP_PROP_CONTRAST)<<endl;
    cout<<CVcap.get(CV_CAP_PROP_SATURATION)<<endl;
    cout<<CVcap.get(CV_CAP_PROP_HUE)<<endl;
    cout<<CVcap.get(CV_CAP_PROP_EXPOSURE)<<endl;
}


bool user_capture::user_cap_save_img(string path)
{
    if(CVcap.isOpened()==false){
        return false;
    }

    // Mat src_image;

    // CVcap.read(src_image);
    // imshow("save img", src_image);
    return imwrite(path, src_image);
}

bool user_capture::user_cap_save_video_start(string path)
{
    if(CVcap.isOpened()==false || is_video_saving==true){
        return false;
    }

    //获取原视频的尺寸
    int a = CVcap.get(CAP_PROP_FRAME_WIDTH);
    int b = CVcap.get(CAP_PROP_FRAME_HEIGHT);
    Size framesize(a,b);
    qDebug()<<"width:"<<a<<"height:"<<b;
    //获取原视频的编码格式
    int fourcc = CVcap.get(CAP_PROP_FOURCC);
    qDebug()<<"编码格式："<<fourcc;

    //获取原视频的帧率
    double fps = CVcap.get(CAP_PROP_FPS);
    qDebug()<<"帧率："<<fps;
    fps = 30;

    if(VWriter.open("e:/xxx.avi", VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, framesize, true)==false){
        qDebug("open false writer");
        is_video_saving = false;
    }else{
        is_video_saving = true;
    }

    return true;
}

void user_capture::user_cap_save_video_stop(void)
{
    if(is_video_saving){
        is_video_saving = false;
        // VWriter.release();
    }
}

bool user_capture::is_user_recording()
{
    return is_video_saving;
}

//=============================================================================
QPixmap user_capture::user_img_open(string path)
{
    src_image = imread(path, IMREAD_COLOR);
    //图像处理
    cvtColor(src_image, src_image, COLOR_BGR2RGB);//需要转换成RGB
    return QPixmap::fromImage(QImage(src_image.data, src_image.cols, src_image.rows, QImage::Format_RGB888));
}



bool user_capture::user_capture::user_video_open(string path)
{
    if(CVcap.isOpened()){
        CVcap.release();
        // cvTimer->stop();
    }
    CVcap.open(path);

    if(CVcap.isOpened()==false){
        return false;
    }

    // cvTimer = new QTimer(this);
    // connect(cvTimer, &QTimer::timeout, this, &user_capture::user_video_read_processing);
    // cvTimer->start(10);

    return true;
}


void user_capture::user_video_read_processing(void)
{
    // Mat src_image;

    CVcap.read(src_image);
    if(src_image.empty()){
        return;
    }

    // imshow("recoding-debug", src_image);

    cvtColor(src_image, src_image, COLOR_BGR2RGB);//需要转换成RGB
    emit user_img_done(QPixmap::fromImage(QImage(src_image.data, src_image.cols, src_image.rows, QImage::Format_RGB888)));
}
